package com.star.sync.elasticsearch.controller;

import com.star.sync.elasticsearch.model.User;
import com.star.sync.elasticsearch.model.UserVo;
import com.star.sync.elasticsearch.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: zhoujunming
 * @Description: TODO
 * @date: 2023/1/10 16:56
 * @email: zhoujunming@bluemoon.com.cn
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    @ResponseBody
    public void add(@RequestParam("count")Integer count) {
        int point = count/5;
        for (int i =0;i<5;i++){
            new Thread(()->{
                for (int j=0;j<point;j++){
                    User user = new User();
                    user.setAge((int)(Math.random()*90 + 10));
                    user.setName(randomString());
                    user.setCreateTime(new Date());
                    userService.insert(user);
                }
            }).start();
        }
    }

    public String randomString (){
        //生成一个包含大小写字母的随机6位字符串；方法1
        String randomcode = "";
        for(int i=0;i<6;i++)
        {
            //52个字母与6个大小写字母间的符号；范围为91~96
            int value = (int)(Math.random()*58+65);
            while(value>=91 && value<=96)
                value = (int)(Math.random()*58+65);
            randomcode = randomcode + (char)value;

        }
        System.out.println(randomcode);

        //用字符数组的方式随机
        String randomcode2 = "";
        String model = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        char[] m = model.toCharArray();

        for (int j=0;j<6 ;j++ )
        {
            char c = m[(int)(Math.random()*52)];
            randomcode2 = randomcode2 + c;
        }

        return   randomcode2;
    }

    //通过年龄查询用户
    @RequestMapping("/searchName")
    public List<User> searchName(@RequestParam(value = "name") String name) throws IOException {
        return userService.searchTerm("name",name);
    }

    //通过年龄查询用户
    @RequestMapping("/searchNameByDB")
    public List<User> searchNameByDB(@RequestParam(value = "name") String name) throws IOException {
        return userService.searchName(name);
    }

    //通过年龄查询用户
    @RequestMapping("/searchDName")
    public List<User> searchDAge(@RequestParam(value = "name") String name) throws IOException {
        return userService.searchDTerm("name",name);
    }

    //分页查询db
    @RequestMapping("/searchPageDB")
    public List<User> searchPageDB(@RequestParam(value = "pageSize") Integer pageSize,
                                    @RequestParam(value = "pageNo")Integer pageNo) throws IOException {
        return userService.searchPageDB(pageSize,pageNo);
    }


    //分页查询es
    @RequestMapping("/searchPageES")
    public List<User> searchPageES(@RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "pageNo")Integer pageNo) throws IOException {
        return userService.searchPageES(pageSize,pageNo);
    }

    //明细查询db
    @RequestMapping("/searchDetailDB")
    public User searchDetailDB(@RequestParam(value = "id") Integer id) throws IOException {
        return userService.searchDetailDB(id);
    }

    //明细查询ES
    @RequestMapping("/searchDetailES")
    public List<User> searchDetailES(@RequestParam(value = "id") String id) throws IOException {
        return userService.searchTerm("id",id);
    }

    //总数统计db
    @RequestMapping("/searchCountDB")
    public int searchCountDB(){
        return userService.searchCountDB();
    }

    //总数统计ES
    @RequestMapping("/searchCountES")
    public Long searchCountES() throws IOException {
        return userService.searchCountES();
    }


    //分组查询db
    @RequestMapping("/searchGroupDB")
    public List<User> searchGroupDB(){
        return userService.searchGroupDB();
    }

    //分组查询ES
    @RequestMapping("/searchGroupES")
    public List<UserVo> searchGroupES() throws IOException {
        return userService.searchGroupES();
    }

    //分页查询es
    @RequestMapping("/searchPageScrollES")
    public List<User> searchPageScrollES(@RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "pageNo")Integer pageNo) throws IOException {
        return userService.searchPageScrollES(pageSize,pageNo);
    }
}
